package org.sidora.strata.context.types;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.TreeSet;
import javax.swing.JPopupMenu;
import org.openide.ErrorManager;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;
import org.openide.util.Utilities;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.sidora.core.Category;
import org.sidora.core.context.DevastatedContext;
import org.sidora.strata.SidoraTools;
import org.sidora.strata.Storage;

/**
 * Top component which displays something.
 */
final class DevastedTopComponent extends TopComponent {
    
    private static DevastedTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    
    private static final String PREFERRED_ID = "DevastedTopComponent";
    
    private DevastedTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(DevastedTopComponent.class, "CTL_DevastedTopComponent"));
        setToolTipText(NbBundle.getMessage(DevastedTopComponent.class, "HINT_DevastedTopComponent"));
//        setIcon(Utilities.loadImage(ICON_PATH, true));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        jTextFieldDestructionTraces = new javax.swing.JTextField();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, "Destruction Traces:");

        jTextFieldDestructionTraces.setEditable(false);
        jTextFieldDestructionTraces.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextFieldDestructionTracesMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel6)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTextFieldDestructionTraces, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(jTextFieldDestructionTraces, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void jTextFieldDestructionTracesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextFieldDestructionTracesMouseClicked
        try {
            Field fiel = DevastatedContext.class.getDeclaredField("destructionTraces");
            jPopupMenu = SidoraTools.generatePopupMenuCategorys(Storage.getContext(), fiel, jTextFieldDestructionTraces);
            jPopupMenu.show(jTextFieldDestructionTraces,evt.getX(),evt.getY());
        } catch (SecurityException ex) {
            ex.printStackTrace();
        } catch (NoSuchFieldException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jTextFieldDestructionTracesMouseClicked
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField jTextFieldDestructionTraces;
    // End of variables declaration//GEN-END:variables
    private JPopupMenu jPopupMenu;
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link findInstance}.
     */
    public static synchronized DevastedTopComponent getDefault() {
        if (instance == null) {
            instance = new DevastedTopComponent();
        }
        return instance;
    }
    
    /**
     * Obtain the DevastedTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized DevastedTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            ErrorManager.getDefault().log(ErrorManager.WARNING, "Cannot find Devasted component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof DevastedTopComponent) {
            return (DevastedTopComponent)win;
        }
        ErrorManager.getDefault().log(ErrorManager.WARNING, "There seem to be multiple components with the '" + PREFERRED_ID + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }
    
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }
    
    public void componentOpened() {
        // TODO add custom code on component opening
    }
    
    public void componentClosed() {
        // TODO add custom code on component closing
    }
    
    /** replaces this in object stream */
    public Object writeReplace() {
        return new ResolvableHelper();
    }
    
    protected String preferredID() {
        return PREFERRED_ID;
    }
    
    final static class ResolvableHelper implements Serializable {
        private static final long serialVersionUID = 1L;
        public Object readResolve() {
            return DevastedTopComponent.getDefault();
        }
    }
        
    public void fillItems() {
        DevastatedContext devastatedContext = (DevastatedContext) Storage.getContext();
        if (devastatedContext != null){
            jTextFieldDestructionTraces.setText(SidoraTools.TreeSet2String(devastatedContext.getDestructionTraces()));
        }
    }
    
    public void emptyItems(){
        
    }
    
}
